//
//  Team9renovatePushViewController.m
//  Team9Project
//
//  Created by dlios on 15/10/17.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9renovatePushViewController.h"
#import "Team9SeeOneViewController.h"
#import "JQAFNetworkTool.h"
#import "Team9dairyCell.h"
#import "Team9dairyHeadModel.h"
#import "Team9dairyDiscussCell.h"
#import "Team9BaseLabel.h"
#import <Masonry.h>
#import "Team9dairyModel.h"
#import "Team9diaryDiscussModel.h"

#import "Team9ExpertCommentModel.h"
#import "Team9ExpertFootCommentView.h"

@interface Team9renovatePushViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *renovateTablew;
@property (nonatomic,strong)  UIImageView *headView;
@property (nonatomic, strong) NSMutableArray *diaryArray;
//@property (nonatomic, strong) NSMutableArray *diaryDiscussArray;
@property (nonatomic, strong) Team9BaseLabel *titleLabel;
@property (nonatomic, strong) Team9BaseLabel *placeLabel;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) Team9BaseLabel *userNameLabel;
@property (nonatomic,strong)  Team9dairyHeadModel *headModel;
@property (nonatomic, strong) Team9dairyModel *dairyModel;
@property (nonatomic, strong) NSMutableArray *picArray;

@property(nonatomic, strong)NSArray<Team9ExpertCommentModel *> *comments;
@end

@implementation Team9renovatePushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title = @"查看详情";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self creatTableView];
    self.empty = [Team9EmptyTool toolWithScrollView:self.renovateTablew];
    [self creatData];
    
        
}

#pragma mark 数据解析
- (void)creatData{
    NSString *str = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page2?col_id=%@&tab=diary", self.bookId];
    
//    [JQAFNetworkTool getUrl:str body:nil result:JQJSON headerFile:nil success:^(id result) {
//        self.headModel = [[Team9dairyHeadModel alloc] init];
//        [self.headModel setValuesForKeysWithDictionary:result[@"data"][@"collection_info"][@"info"]];
//        [self giveHeadValue];
//        
//        // riji
//        self.diaryArray = [NSMutableArray array];
//        self.diaryArray = [Team9dairyModel baseModelByArr:result[@"data"][@"collection_diaries"][@"diaries"]].mutableCopy;
//        
//        // 讨论
//        self.diaryDiscussArray = [NSMutableArray array];
//        self.diaryDiscussArray = [Team9diaryDiscussModel baseModelByArr:result[@"data"][@"collection_comments"][@"comments"]].mutableCopy;
//        
//        
//        [self.renovateTablew reloadData];
//        
//    } failure:^(NSError *error) {
//        
//        
//    }];
    
    
    [Team9RefreshTool getUrl:str success:^(id result) {
        
       
            self.headModel = [[Team9dairyHeadModel alloc] init];
            [self.headModel setValuesForKeysWithDictionary:result[@"data"][@"collection_info"][@"info"]];
            [self giveHeadValue];
            
            // riji
            self.diaryArray = [NSMutableArray array];
            self.diaryArray = [Team9dairyModel baseModelByArr:result[@"data"][@"collection_diaries"][@"diaries"]].mutableCopy;
            
            // 讨论
//            self.diaryDiscussArray = [NSMutableArray array];
//            self.diaryDiscussArray = [Team9diaryDiscussModel baseModelByArr:result[@"data"][@"collection_comments"][@"comments"]].mutableCopy;
        self.comments = [Team9ExpertCommentModel baseModelByArr:result[@"data"][@"collection_comments"][@"comments"]];
            
            [self.renovateTablew reloadData];
        
        } scrollView:self.renovateTablew empty:self.empty];
}



- (void)giveHeadValue{
    self.titleLabel.text = self.headModel.col_name;
    self.placeLabel.text = self.headModel.title_sub_3;
    [Team9Tool sd_imageView:self.headImageView image:self.headModel.author_face placeholder:[UIImage imageNamed:@"111"]];
    self.userNameLabel.text = self.headModel.author_nick;
    [Team9Tool sd_imageView:self.headView image:self.headModel.cover placeholder:[UIImage imageNamed:@"111"]];
    [Team9Tool imageView:self.headImageView type:self.headModel.author_type Id:self.headModel.author_id viewController:self baiHiden:NO];

}
#pragma mark 创建tableView 和头视图

- (void)creatTableView{

    self.renovateTablew = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEGHT - 64) style:UITableViewStylePlain];
    self.renovateTablew.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.renovateTablew];
//    self.renovateTablew.bounces = NO;
    self.renovateTablew.delegate = self;
    self.renovateTablew.dataSource = self;
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    self.renovateTablew.tableFooterView = view;
    
    self.headView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 180 / 375.0 * WIDTH)];
    self.headView.backgroundColor = [UIColor clearColor];
    self.renovateTablew.tableHeaderView = self.headView;
    self.headView.userInteractionEnabled = YES;
    
    self.titleLabel = [[Team9BaseLabel alloc] init];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.font = [UIFont systemFontOfSize:18 / 375.0 * WIDTH];
    [self.headView addSubview:self.titleLabel];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.placeLabel = [[Team9BaseLabel alloc] init];
    self.placeLabel.backgroundColor = [UIColor clearColor];
    self.placeLabel.font =[ UIFont systemFontOfSize:15 / 375.0 * WIDTH];
    [self.headView addSubview:self.placeLabel];
    self.placeLabel.textColor = [UIColor whiteColor];
    self.placeLabel.textAlignment = NSTextAlignmentCenter;
    
    self.headImageView = [[UIImageView alloc] init];
    [self.headView addSubview:self.headImageView];
    self.headImageView.layer.cornerRadius = 25 / 375.0 * WIDTH;
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.backgroundColor = [UIColor clearColor];
    
    self.userNameLabel = [[Team9BaseLabel alloc] init];
    [self.headView addSubview:self.userNameLabel];
    self.userNameLabel.backgroundColor = [UIColor clearColor];
    self.userNameLabel.font = [UIFont systemFontOfSize:13 / 375.0 * WIDTH];
    self.userNameLabel.textAlignment = NSTextAlignmentCenter;
    self.userNameLabel.textColor = [UIColor whiteColor];
    
    
    [self make_Constraints];
}


- (void)make_Constraints{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headView.mas_centerX);
        make.centerY.equalTo(self.headView.mas_centerY).offset(-40 / 375.0 * WIDTH);
        make.width.equalTo(self.headView.mas_width).offset(-40 / 375.0 * WIDTH);
        make.height.mas_equalTo(30 / 375.0 * WIDTH);
    
    }];
    
    [self.placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headView.mas_centerX);
        make.centerY.equalTo(self.headView.mas_centerY).offset(-10 / 375.0 * WIDTH);
        make.width.equalTo(self.headView.mas_width).offset(-40 / 375.0 * WIDTH);
        make.height.mas_equalTo(25 / 375.0 * WIDTH);
        
    }];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headView.mas_centerX);
        make.centerY.equalTo(self.headView.mas_centerY).offset(30 / 375.0 * WIDTH);
        make.width.mas_equalTo(50 / 375.0 * WIDTH);
        make.height.equalTo(self.headImageView.mas_width);
        
    }];
    
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headView.mas_centerX);
        make.top.mas_equalTo(self.headImageView.mas_bottom).offset(5 / 375.0 * WIDTH);
        make.width.equalTo(self.headView.mas_width).offset(-40 / 375.0 * WIDTH);
        make.height.mas_equalTo(18 / 375.0 * WIDTH);
        
    }];
}



#pragma mark tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
   
    if (section == 0) {
         return self.diaryArray.count;
    }else if (section == 1){
//        return self.diaryDiscussArray.count;
        return self.comments.count;
    }
    return 0;
    
}

// 分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0 && _diaryArray.count) {
        return @"日记";
//    }else if (section == 1 && _diaryDiscussArray.count){
    } else if (section == 1 && _comments.count){
        return @"评论";
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        if (self.diaryArray.count == 0) {
            return 0;
        }
    }else{
//        if (self.diaryDiscussArray.count == 0) {
        if (_comments.count == 0) {
            return 0;
        }
//            return 0;
//        }
    
    }
    return 30 / 667.0 * HEGHT;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        Team9dairyModel *model = self.diaryArray[indexPath.row];
        
        
        if (model.content != nil) {
            
           CGFloat contentHeight = [Team9Tool heightByFloatSize:13 / 375.0 * WIDTH  width:WIDTH - 40 / 375.0 * WIDTH With:model.content];

           if (model.pics.count != 0) {
            
              return contentHeight + 200 / 375.0 * WIDTH;
         
           } return contentHeight + 100 / 375.0 * WIDTH;
            
        }else{
    
            return 100 / 375.0 * WIDTH;
        }
        
        
    }else if (indexPath.section == 1)
    {
        CGFloat width = WIDTH - 100 * WIDTH / 375 - 20;
        Team9ExpertCommentModel *model = self.comments[indexPath.row];
        if (model.comment_parent_id.boolValue) {
            width -= 40 * WIDTH/ 375 + 20;
        }
        CGFloat height = [Team9Tool heightByFloatSize:14 * WIDTH / 375 width:width With:model.content] + 25 * WIDTH / 375 + 20;
        CGFloat lessHeight = 40 * WIDTH / 375 + 20;
        return height > lessHeight ? height : lessHeight;
        
//        if (self.diaryDiscussArray.count != 0) {
//            Team9diaryDiscussModel *disModel = self.diaryDiscussArray[indexPath.row];
////            CGFloat contentHeight = 0;
//            CGFloat contentHeight = [Team9Tool heightByFloatSize:13 / 375.0 * WIDTH width:WIDTH - 40 / 375.0 * WIDTH With:disModel.content];
//            
//            return contentHeight + 80 / 375.0 * WIDTH;

//        }
        
}
  
    return 0;
}



- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        static NSString *reuse = @"reuse";
        Team9dairyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell= [[Team9dairyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        
        Team9dairyModel *dairyModel = self.diaryArray[indexPath.row];
        cell.dairyModel = dairyModel;
        cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        __weak __typeof(self)weakSelf = self;
        void (^Block)(NSInteger ) = ^(NSInteger a){
            if (a == 1000 && dairyModel.pics.count > 0) {
                
                self.picArray = @[].mutableCopy;
                
                Team9SeeOneViewController *seeVC = [[Team9SeeOneViewController alloc] init];
                
//                Team9dairyModel *dairyModel = self.diaryArray[indexPath.row];
                
                for (NSInteger con = 0; con < dairyModel.pics.count; con ++) {
                    
                    [self.picArray addObject:dairyModel.pics[con][@"pic"]];
                    
                }
                seeVC.picArray = [NSArray arrayWithArray:self.picArray];
                seeVC.number = 0;
                [weakSelf presentViewController:seeVC animated:YES completion:^{
                    
                    
                }];
                
                
            }else if (a == 1001 && dairyModel.pics.count > 1){
                
                self.picArray = @[].mutableCopy;
                
                Team9SeeOneViewController *seeVC = [[Team9SeeOneViewController alloc] init];
                
                
//                Team9dairyModel *dairyModel = self.diaryArray[indexPath.row];
                
                for (NSInteger con = 0; con < dairyModel.pics.count; con ++) {
                    
                    [self.picArray addObject:dairyModel.pics[con][@"pic"]];
                 
                    
                }
                seeVC.picArray = [NSArray arrayWithArray:self.picArray];
                seeVC.number = 1;
                
                [weakSelf presentViewController:seeVC animated:YES completion:^{
                    
                    
                }];
                
                
                
            }else if (a == 1002 && dairyModel.pics.count > 2){
                
                self.picArray = @[].mutableCopy;
                
                Team9SeeOneViewController *seeVC = [[Team9SeeOneViewController alloc] init];
                
//                 Team9dairyModel *dairyModel = self.diaryArray[indexPath.row];
                
                for (NSInteger con = 0; con < dairyModel.pics.count; con ++) {
                    
                    [self.picArray addObject:dairyModel.pics[con][@"pic"]];
                    
                }
                
                seeVC.picArray = [NSArray arrayWithArray:self.picArray];
                seeVC.number = 2;
                
                [weakSelf presentViewController:seeVC animated:YES completion:^{
                    
                    
                }];
                
                
                
            }else if (a == 1003 && dairyModel.pics.count > 3){
                
                self.picArray = @[].mutableCopy;
                
                Team9SeeOneViewController *seeVC = [[Team9SeeOneViewController alloc] init];
                
//               Team9dairyModel *dairyModel = self.diaryArray[indexPath.row];
                for (NSInteger con = 0; con < dairyModel.pics.count; con ++) {
                    
                    [self.picArray addObject:dairyModel.pics[con][@"pic"]];
                    
                }
                seeVC.picArray = [NSArray arrayWithArray:self.picArray];
                seeVC.number = 3;
                
                [weakSelf presentViewController:seeVC animated:YES completion:^{
                    
                    
                }];
                
                
            }
            
        };
        cell.block = Block;

        
        
        
        
        return  cell;
    }else{
        Team9ExpertFootCommentView *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseCell"];
        if (!cell) {
            cell = [[Team9ExpertFootCommentView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseCell"];
        }
        cell.vc = self;
        cell.model = self.comments[indexPath.row];
//        return cell;
//        static NSString *reuse = @"Reuse";
//        Team9dairyDiscussCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
//        if (!cell) {
//            cell= [[Team9dairyDiscussCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
//        }
//        
//        if (self.diaryDiscussArray.count) {
//            Team9diaryDiscussModel *disModel = self.diaryDiscussArray[indexPath.row];
//            cell.vc = self;
//            cell.disModel = disModel;
//        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return  cell;
        
    }
    
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
